package com.example.networklab.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.example.networklab.common.ResultResponse;
import com.example.networklab.domain.query.TeaTaskPageQuery;
import com.example.networklab.domain.query.TeaTaskRecordPageQuery;
import com.example.networklab.domain.req.AddOrEditTaskReq;
import com.example.networklab.service.IStudentGradeService;
import com.example.networklab.service.ITaskHistoryService;
import com.example.networklab.service.ITaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "*")
@RestController
@RequiredArgsConstructor
@SaCheckLogin
@SaCheckRole("teacher")
public class TeacherController {
    private final ITaskService taskService;
    private final ITaskHistoryService taskHistoryService;
    private final IStudentGradeService studentGradeService;

    //教师新增作业
    @PostMapping("/tea/task")
    public ResultResponse addTask(@RequestBody AddOrEditTaskReq addTaskRequest) {
        taskService.addTask(addTaskRequest);
        return ResultResponse.success();
    }

    //教师修改作业
    @PutMapping("/tea/task/{taskId}")
    public ResultResponse editTask(@PathVariable("taskId") Long taskId, @RequestBody AddOrEditTaskReq addTaskRequest) {
        taskService.editTask(taskId, addTaskRequest);
        return ResultResponse.success();
    }

    //教师分页查看已经发布的作业
    @GetMapping("/tea/task/all")
    public ResultResponse getTaskList(TeaTaskPageQuery teaTaskPageQuery) {
        return ResultResponse.success(taskService.teaGetTaskList(teaTaskPageQuery));
    }

    //教师分页查看某次作业的提交记录
    @GetMapping("/tea/task/record/{taskId}")
    public ResultResponse getTaskRecord(@PathVariable("taskId") Long taskId, TeaTaskRecordPageQuery pageQuery) {
        return ResultResponse.success(taskHistoryService.teaGetTaskRecord(taskId, pageQuery));
    }

    //教师查看已经发布的作业的描述和测试用例
    @GetMapping("/tea/task/desccase/{taskId}")
    public ResultResponse getTaskDescCase(@PathVariable("taskId") Long taskId) {
        return ResultResponse.success(taskService.getTaskDescCase(taskId));
    }

    //教师查看作业的成绩单
    @GetMapping("/tea/task/grade/{taskId}")
    public ResultResponse teaGetTaskGrade(@PathVariable("taskId") Long taskId) {
        return ResultResponse.success(studentGradeService.teaGetTaskGrade(taskId));
    }

    //教师删除作业
    @DeleteMapping("/tea/task/{taskId}")
    public ResultResponse deleteTask(@PathVariable("taskId") Long taskId) {
        taskService.removeTask(taskId);
        return ResultResponse.success();
    }

    //教师查看某次提交的配置内容
    @GetMapping("/tea/task/history/config/{id}")
    public ResultResponse getTaskHistoryConfig(@PathVariable("id") Long id) {
        return ResultResponse.success(taskHistoryService.getTaskHistoryConfig(id));
    }

}
